home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc / OpenDoc Development / Debugging Support / OpenDoc™ Source Code / Storage / Bento / CM / Handlers.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-08-28  |  7.7 KB  |  211 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Handlers.c
  3.  
  4.     Contains:    Container Manager Handlers Routines
  5.  
  6.     Written by:    Ira L. Ruben
  7.  
  8.     Owned by:    Ed Lai
  9.  
  10.     Copyright:    © 1991-1994 by Apple Computer, Inc., all rights reserved.
  11.  
  12.     Change History (most recent first):
  13.  
  14.          <2>     8/26/94    EL        #1182319 Don't allocate memory for looking
  15.                                     up symbol
  16.          <2>      2/3/94    EL        Bento File names are now eight chars or
  17.                                     less.
  18.  
  19.     To Do:
  20. */
  21.  
  22. /*---------------------------------------------------------------------------*
  23.  |                                                                           |
  24.  |                            <<< Handlers.c >>>                             |
  25.  |                                                                           |
  26.  |                    Container Manager Handlers Routines                    |
  27.  |                                                                           |
  28.  |                               Ira L. Ruben                                |
  29.  |                                 11/18/91                                  |
  30.  |                                                                           |
  31.  |                  Copyright Apple Computer, Inc. 1991-1994                 |
  32.  |                           All rights reserved.                            |
  33.  |                                                                           |
  34.  *---------------------------------------------------------------------------*
  35.  
  36.  The routines in this file maintain handler/type name associations.  Handlers are defined
  37.  in terms of their type.  They may be retrieved by using that type.
  38. */
  39.  
  40.  
  41. #include <stddef.h>
  42. #include <string.h>
  43. #include <stdio.h>
  44.  
  45. #ifndef __CMTYPES__
  46. #include "CMTypes.h"
  47. #endif
  48. #ifndef __CM_API__
  49. #include "CMAPI.h"
  50. #endif
  51. #ifndef __SYMMGR__
  52. #include "SymTbMgr.h"      
  53. #endif
  54. #ifndef __SESSIONDATA__
  55. #include "Session.h"          
  56. #endif
  57. #ifndef __HANDLERS__
  58. #include "Handlers.h"
  59. #endif
  60. #ifndef __UTILITYROUTINES__
  61. #include "Utility.h"        
  62. #endif
  63.  
  64.                                                                     CM_CFUNCTIONS
  65.  
  66. /* The following generates a segment directive for Mac only due to 32K Jump Table             */
  67. /* Limitations.  If you don't know what I'm talking about don't worry about it.  The        */
  68. /* default is not to generate the pragma.  Theoritically unknown pragmas in ANSI won't    */
  69. /* choke compilers that don't recognize them.  Besides why would they be looked at if        */
  70. /* it's being conditionally skipped over anyway?  I know, famous last words!                        */
  71.  
  72. #if CM_MPW
  73. #pragma segment Handlers
  74. #endif
  75.  
  76.  
  77. /*-----------------------------------------------------------------*
  78.  | enterCompare - type name comparison routine for cmEnterSymbol() |
  79.  *-----------------------------------------------------------------*
  80.  
  81.  This routine is "sent" to cmEnterSymbol() to do the proper comparisons for handlers.  The
  82.  handler tree is based on their type names.  cmEnterSymbol() is a generic binary tree
  83.  routine that requires the comparsion to be supplied by its caller to decide on what
  84.  basis the tree is build.  Hence this routine!
  85.  
  86.  Note, this "static" is intentionally left to default memory model under DOS since it is
  87.  passed as a function pointer to cmEnterSymbol().
  88. */
  89.  
  90. static int enterCompare(const void *h1, const void *h2)
  91. {
  92.     return (strcmp((CM_CHAR *)((MetaHandlerPtr)h1)->typeName,
  93.                                  (CM_CHAR *)((MetaHandlerPtr)h2)->typeName));
  94. }
  95.  
  96. /*-------------------------------------------------------------------*
  97.  | lookupCompare - type name comparison routine for cmLookupSymbol() |
  98.  *-------------------------------------------------------------------*
  99.  
  100.  This routine is "sent" to cmLookupSymbol() to do the proper comparisons for global names.
  101.  The global name tree is based on the names (obviously).  cmLookupSymbol() is a generic
  102.  binary tree routine that requires the comparsion to be supplied by its caller to decide
  103.  on what basis the tree is build.  Hence this routine!
  104.  
  105.  Note, this "static" is intentionally left to default memory model under DOS since it is
  106.  passed as a function pointer to cmEnterSymbol().
  107. */
  108.  
  109. static int lookupCompare(const void *h1, const CM_UCHAR *name)
  110. {
  111.     return (strcmp((CM_CHAR *)((MetaHandlerPtr)h1)->typeName, (CM_CHAR *)name));
  112. }
  113.  
  114.  
  115. /*--------------------------------------------------*
  116.  | cmDefineMetaHandler - define a new "metahandler" |
  117.  *--------------------------------------------------*
  118.  
  119.  Define a new metahandler with the specifed type (a C string). The function returns a
  120.  pointer to the new handler or, if dup is true, a pointer to a previously defined entry.
  121.  
  122.  If NULL is returned then there was an allocation failure and the new type could not be
  123.  created.
  124.  
  125.  Note, the global data session pointer created by CMStartSession() is passed since
  126.  methandlers are global to all containers and thus the root of the metahandler symbol
  127.  table is kept as part of the session data.
  128. */
  129.  
  130. MetaHandlerPtr cmDefineMetaHandler(CMMetaHandler metaHandler, 
  131.                                                                       const CM_UCHAR *typeName,
  132.                                                                         CMBoolean *dup,
  133.                                                                      SessionGlobalDataPtr sessionData)
  134. {
  135.     MetaHandlerPtr h, newMetaHandler;
  136.     
  137.     if ((newMetaHandler = (MetaHandlerPtr)SessionMalloc(sizeof(MetaHandler) + strlen((CM_CHAR *)typeName))) != NULL) {
  138.         strcpy((CM_CHAR *)newMetaHandler->typeName, (CM_CHAR *)typeName);    /* fill in new entry        */
  139.         newMetaHandler->metaHandler = metaHandler;
  140.         
  141.         h = (MetaHandlerPtr)cmEnterSymbol(newMetaHandler, (void **)&SessionMetaHandlerTable, dup, enterCompare);
  142.         
  143.         if (*dup) SessionFree(newMetaHandler);                                            /* oops!                                */
  144.     } else {
  145.         h = NULL;
  146.         *dup = false;
  147.     }
  148.     
  149.     return (h);                                                                                                        /* return entry ptr            */
  150. }
  151.  
  152.  
  153. /*---------------------------------------------------------------*
  154.  | cmLookupMetaHandler - find a previously defined "metahandler" |
  155.  *---------------------------------------------------------------*
  156.  
  157.  Find a metahandler associated with the specified type.  If found, the HandlerPtr to the
  158.  found entry is returned (which includes the handler proc and type).  If not found NULL
  159.  is returned.
  160.  
  161.  Note, we allocate a temporary handler table entry here and then free it.   If the
  162.  allocation fails, SessionSuccess, a session status switch, is returned false.  Otherwise
  163.  SessionSuccess is true.
  164.     
  165.  We used to share the same compare routine for lookup and enter symbol, but that
  166.  involves allocation of temporary memory. That is not very inefficient. So now we
  167.  have separate compare routine for lookup and enter symbol to elimiate the temporary
  168.  memory allocation.
  169. */
  170.  
  171. MetaHandlerPtr cmLookupMetaHandler(const CM_UCHAR *typeName,
  172.                                                                      SessionGlobalDataPtr sessionData)
  173. {
  174.     sessionData->success = true;
  175.     return (MetaHandlerPtr)(cmLookupSymbol(typeName, SessionMetaHandlerTable, lookupCompare));
  176. }
  177.  
  178.  
  179. /*-------------------------------------------------------------*
  180.  | cmForEachMetaHandler - do some action on each "metahandler" |
  181.  *-------------------------------------------------------------*
  182.  
  183.  Do (call) the specified action for each defined metahandler in the current session. The
  184.  pointer to each metahandler entry is passed to the action routine along with a "refCon"
  185.  which the caller can use as a communication facility to convey additional info to the
  186.  action routine.
  187. */
  188.  
  189. void cmForEachMetaHandler(CMRefCon refCon, 
  190.                                                     void (*action)(MetaHandlerPtr aHandler, CMRefCon refCon),
  191.                                                     SessionGlobalDataPtr sessionData)    
  192. {
  193.     cmForEachSymbol(SessionMetaHandlerTable, refCon, (SymbolAction)action);
  194. }
  195.  
  196.  
  197. /*-------------------------------------------------*
  198.  | cmFreeAllMetaHandlers - free all "metahandlers" |
  199.  *-------------------------------------------------*
  200.  
  201.  This routine is called to remove the definitions of ALL previously defined metahandlers
  202.  for the current session.
  203. */
  204.  
  205. void cmFreeAllMetaHandlers(SessionGlobalDataPtr sessionData)
  206. {
  207.     cmFreeAllSymbols((void **)&SessionMetaHandlerTable, sessionData); /* just glue code        */
  208. }
  209.                                                           
  210.                                                             CM_END_CFUNCTIONS
  211.